package com.lvyou.ctrl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lvyou.entity.Goods;
import com.lvyou.model.JsonResult;
import com.lvyou.service.GoodsSrv;
import com.lvyou.util.TurnPage;

@RestController
public class GoodsCtrl {

	@Resource(name="GoodsSrv")
	private GoodsSrv srv;
	
	@RequestMapping("/goods/list")
	public JsonResult list(String name, Integer lowPrice, Integer highPrice, Integer status, @ModelAttribute("turnPage") TurnPage turnPage, HttpServletRequest request){
		System.out.println(request.getSession().getId());
		System.out.println(request.getMethod());

		JsonResult jr=new JsonResult();

		if(status==null){
			status=1;
		}
		
		try {
			List<Goods> list=srv.queryList(name, lowPrice, highPrice,status, turnPage);
			
			List<Map> dataList=new ArrayList(list.size());
			
			for(Goods goods:list) {
				Map map=PropertyUtils.describe(goods);
				map.remove("class");
				String tags=(String)map.get("tags");
				String services=(String)map.get("services");
				
				if(tags!=null && !"".equals(tags)) {
					String[] tagArray=tags.split(",");
					map.put("tagArray", tagArray);
				}
				if(services!=null && !"".equals(services)) {
					String[] serviceArray=services.split(",");
					map.put("serviceArray", serviceArray);
				}
				
				dataList.add(map);
			}
			
			jr.setCode(0);
			jr.setData(dataList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jr.setCode(100000);
			jr.setMsg("发生错误");
		}
		
		return jr;
	}
	
	@RequestMapping("/detail")
	public ModelAndView showGoodsById(String id,ModelAndView mv){
		try {
			Goods goods=srv.showGoodsById(id);

			mv.addObject("goods", goods);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mv.setViewName("detail");
		return mv;
	}
	@RequestMapping("/api/detail")
	public JsonResult showGoodsByIdAPI(String id){
		JsonResult jr=new JsonResult();
		try {
			Goods goods=srv.showGoodsById(id);
			Map map=PropertyUtils.describe(goods);
			map.remove("class");
			String tags=(String)map.get("tags");
			if(tags!=null && !"".equals(tags)){
				String[] tagArray=tags.split(",");
				map.put("tags",tagArray);
			}
			String services=goods.getServices();
			if(services!=null && !"".equals(services)){
				String[] serviceArray=services.split(",");
				map.put("services",serviceArray);
			}
			Date date=goods.getUpdateTime();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			map.put("updateTime",sdf);

			jr.setCode(0);
			jr.setData(map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jr.setCode(100000);
			jr.setMsg("发生错误");
		}

		return jr;
	}
}
